package controllers

import (
	"encoding/json"
	"ltbz/cultural_tourism_api/models"
	"ltbz/cultural_tourism_api/utils"
	"time"
)

// 预约管理
type ReserveManageController struct {
	BaseAuthController
}

// @Title 添加预约管理
// @Description 添加预约管理
// @Param	request	body models.ReserveManageAddReq true "type json string"
// @Success 200
// @router /add [post]
func (this *ReserveManageController) Add() {
	br := new(models.BaseResponse).Init()
	defer func() {
		this.Data["json"] = br
		this.ServeJSON()
	}()
	var req models.ReserveManageAddReq
	err := json.Unmarshal(this.Ctx.Input.RequestBody, &req)
	if err != nil {
		br.Msg = "参数解析异常！"
		br.ErrMsg = "参数解析失败，Err:" + err.Error()
		return
	}

	if req.Mobile == "" {
		br.Msg = "手机号不能为空"
		return
	}

	var condition string
	var pars []interface{}
	condition += ` AND mobile = ? `
	pars = append(pars, req.Mobile)

	obj := new(models.ReserveManage)

	count, err := obj.GetReserveManageCount(condition, pars)
	if err != nil {
		br.Msg = "新增失败！"
		br.ErrMsg = "新增失败，判断手机号是否已预约失败，Err:" + err.Error()
		return
	}

	if count > 0 {
		br.Msg = "手机号已预约！"
		return
	}
	reserveNum := utils.GetRandString(7)
	obj.Mobile = req.Mobile
	obj.ReserveNum = reserveNum
	obj.CreateTime = time.Now()
	obj.ModifyTime = time.Now()
	err = obj.AddReserveManage()
	if err != nil {
		br.ErrMsg = "保存失败，Err:" + err.Error()
		br.Msg = "保存失败"
		return
	}
	br.Msg = "保存成功"
	br.Ret = 200
	return
}

// @Title 获取预约管理列表
// @Description 获取预约管理列表
// @Param	request	body models.ReserveManageListReq true "type json string"
// @Success 200 {object} models.ReserveManagePageResp
// @router /list [post]
func (this *ReserveManageController) List() {
	br := new(models.BaseResponse).Init()
	defer func() {
		this.Data["json"] = br
		this.ServeJSON()
	}()
	body := this.Ctx.Input.RequestBody
	req := new(models.ReserveManageListReq)
	if string(body) != "" {
		err := json.Unmarshal(this.Ctx.Input.RequestBody, &req)
		if err != nil {
			br.Msg = "参数解析异常！"
			br.ErrMsg = "参数解析失败，Err:" + err.Error()
			return
		}
	}

	if req.PageSize <= 0 {
		req.PageSize = utils.PageSize20
	}
	if req.CurrentIndex <= 0 {
		req.CurrentIndex = 1
	}

	startSize := utils.StartIndex(req.CurrentIndex, req.PageSize)

	obj := new(models.ReserveManage)

	var condition string
	var pars []interface{}

	if req.Mobile != "" {
		scenicSpotName := `%` + req.Mobile + `%`
		condition += "AND mobile LIKE ?"
		pars = append(pars, scenicSpotName)

	}

	total, err := obj.GetReserveManageCount(condition, pars)
	if err != nil {
		br.Msg = "获取数据失败！"
		br.ErrMsg = "获取数据失败，Err:" + err.Error()
		return
	}

	list, err := obj.GetReserveManagePage(condition, pars, startSize, req.PageSize)
	if err != nil {
		br.Msg = "获取数据失败！"
		br.ErrMsg = "获取数据失败，Err:" + err.Error()
		return
	}

	page := utils.GetPaging(req.CurrentIndex, req.PageSize, total)
	resp := new(models.ReserveManagePageResp)
	resp.List = list
	if err != nil {
		br.Msg = "获取数据失败"
		br.ErrMsg = "获取数据失败，Err:" + err.Error()
		return
	}
	resp.Paging = page

	br.Msg = "获取成功"
	br.Ret = 200
	br.Data = resp
	return
}

// @Title 删除预约管理
// @Description 删除预约管理
// @Param	request	body models.ReserveManageDelReq true "type json string"
// @Success 200
// @router /delete [post]
func (this *ReserveManageController) Delete() {
	br := new(models.BaseResponse).Init()
	defer func() {
		this.Data["json"] = br
		this.ServeJSON()
	}()
	var req models.ReserveManageDelReq
	err := json.Unmarshal(this.Ctx.Input.RequestBody, &req)
	if err != nil {
		br.Msg = "参数解析异常！"
		br.ErrMsg = "参数解析失败，Err:" + err.Error()
		return
	}
	if req.ReserveManageId <= 0 {
		br.Msg = "参数错误"
		return
	}
	obj := new(models.ReserveManage)
	err = obj.DeleteReserveManage(req.ReserveManageId)
	if err != nil {
		br.Msg = "删除失败！"
		br.ErrMsg = "删除失败，Err:" + err.Error()
		return
	}
	br.Msg = "删除成功！"
	br.Ret = 200
	return
}
